#!/usr/bin/python
# -*- coding: utf8 -*-

import sys
import os
import gc

from sentence import *
from rule import *
from grammar import *
from algorithm import *

if __name__ == '__main__':
    gc.set_debug(gc.DEBUG_LEAK)
    load_rules(sys.argv[1])
    
    input_file = open(sys.argv[2], 'r')
    lines = unicode(input_file.read(), 'gb18030').split('\n')

    count = 0
    grammar = Grammar()
    for line in lines:
        if line == "":
            continue

        sentence = Sentence()  
        sentence.parse_corpus(line)
        
        grammar.parse_sentence(sentence)
        # grammar.log(output_file)

        inside_probs = inside(grammar)
        outside_probs = outside(grammar, inside_probs)

        for i in range(0, len(grammar.nodes)):
                print node_string(grammar.nodes[i]), inside_probs[i]

        count += 1
